//
//  YHMachineManagementSubViewController.m
//  YHApp
//
//  Created by -- on 2021/12/19.
//

#import "HAMachineSubVC.h"
#import "HAAllMachineListVC.h"
#import "HAMachinePullDownVC.h"
#import "HAMachinePullDownRecordVC.h"
#import "HAMachineCallbackVC.h"
#import "HAMachineHomeMo.h"
#import "HACanalItemMo.h"

@interface HAMachineSubVC ()

/***/
@property (nonatomic, strong) UIView *mainV;
/***/
@property (nonatomic, strong) NSMutableArray *numLabelArray;
/***/
@property (nonatomic, strong) NSMutableArray *titLabelArray;
/***/
@property (nonatomic, strong) UILabel *machineNumLabel;
/***/
@property (nonatomic, strong) HAMachineHomeMo *model;
/***/
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation HAMachineSubVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.items = [NSMutableArray array];
    [self setupUI];
    [self loadData];
    

    // Do any additional setup after loading the view.
}

- (void)loadData {
//    SYRequest *request = [[SYRequest alloc]init];
//    request.requestMethod = SYRequestMethodGET;
//    request.requestUrl = @"sns/index";
//    request.parms = @{
//        @"source_id":self.canalItemModel.value,
//    };
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"sns/chart";
    request.parms = @{
        @"source_id":self.canalItemModel.value ?: @"",
    };
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
//                self.model = [YHMachineManagementHomeModel mj_objectWithKeyValues:dataModel.data];
//                self.machineNumLabel.text = self.model.allCount;
//                self.s1NL.text = self.model.uninstallCount;
//                self.s2NL.text = self.model.installCount;
//                self.s3NL.text = self.model.notActive;
//                self.s4NL.text = self.model.activeCount;
                
                if ([[dataModel.data allKeys]containsObject:@"allCount"]) {
                    NSString *allCount = [NSString stringWithFormat:@"%@",dataModel.data[@"allCount"]];
                    self.machineNumLabel.text = allCount;
                }
                
                if ([[dataModel.data allKeys]containsObject:@"itemall"]) {
                    [self.items removeAllObjects];
                    NSArray *items = dataModel.data[@"itemall"];
                    [self.items addObjectsFromArray:items];
                    for (int i = 0; i < items.count; i ++) {
                        NSDictionary *dic = items[i];
                        if (self.titLabelArray.count > i && self.numLabelArray.count > i) {
                            UILabel *titLabel = self.titLabelArray[i];
                            UILabel *numLabel = self.numLabelArray[i];
                            titLabel.text = dic[@"label"];
                            numLabel.text = [NSString stringWithFormat:@"%@",dic[@"number"]];
                        }
                    }
                }
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}


-(void)itemViewClick:(UITapGestureRecognizer *)tap {
    UIView *view = tap.view;

    if (view.tag == 0) {
        HAAllMachineListVC *vc = [[HAAllMachineListVC alloc]init];
        vc.allCount = self.model.allCount;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (view.tag == 1) {
        HAMachinePullDownVC *vc = [[HAMachinePullDownVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (view.tag == 2) {
        HAMachinePullDownRecordVC *vc = [[HAMachinePullDownRecordVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (view.tag == 3) {
//        [MBProgressHUD showError:@"敬请期待"];
        HAMachineCallbackVC *vc = [[HAMachineCallbackVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)setupUI {
        
    UIView *topBgV = [UIView ha_createView];
    topBgV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topBgV];
    [topBgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_offset(220);
    }];
    
    UIView *machineNumBgView = [UIView ha_createView];
    machineNumBgView.backgroundColor = [UIColor colorWithHexString:@"#E6F1FD"];
    machineNumBgView.layer.cornerRadius = 3;
    machineNumBgView.layer.masksToBounds = YES;
    [topBgV addSubview:machineNumBgView];
    [machineNumBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(20);
        make.height.mas_offset(40);
    }];
    
    UILabel *machineNumTitLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#000000" text:@"机具总量" alignment:0];
    [machineNumBgView addSubview:machineNumTitLabel];
    [machineNumTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];

    UILabel *machineNumLabel = [UILabel ha_BLabelWithFont:14 textColor:@"#000000" text:@"0" alignment:2];
    self.machineNumLabel = machineNumLabel;
    [machineNumBgView addSubview:machineNumLabel];
    [machineNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(0);
    }];
    
    [topBgV addSubview:self.mainV];
    [self.mainV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(machineNumBgView.mas_bottom).offset(10);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-20);
    }];
    ///统计
    CGFloat sp = 6;
    CGFloat modViewX = 12;
    CGFloat modViewY = 12;
    CGFloat modViewW = (kUIScreen_w-24-sp-sp)/3;
    CGFloat modViewH = 66;
    NSArray *titleLs = @[@"未绑定",@"已绑定",@"伪激活",@"已激活",@"未达标",@"已达标"];
    [self.titLabelArray removeAllObjects];
    [self.numLabelArray removeAllObjects];

    for (int i = 0; i < titleLs.count; i ++) {
        NSString *title = titleLs[i];
        UIColor *bgColor;
        if (i == 0 || i == 3) {
            bgColor = [UIColor colorWithHexString:@"#4876F1"];
        }
        if (i == 1 || i == 4) {
            bgColor = [UIColor colorWithHexString:@"#6AE176"];
        }
        if (i == 2 || i == 5) {
            bgColor = [UIColor colorWithHexString:@"#E95D5E"];
        }
        if (i == 1) {
            modViewX = 12 + modViewW + sp;
        }
        if (i == 2) {
            modViewX = 12 + modViewW + sp + modViewW + sp;
        }
        if (i == 3) {
            modViewX = 12;
            modViewY = 12 + modViewH + sp;
        }
        if (i == 4) {
            modViewX = 12 + modViewW + sp;
            modViewY = 12 + modViewH + sp;
        }
        if (i == 5) {
            modViewX = 12 + modViewW + sp + modViewW + sp;
            modViewY = 12 + modViewH + sp;
        }
        UIView *modView = [self getModView:i title:title bgColor:bgColor];
        modView.frame = CGRectMake(modViewX, modViewY, modViewW, modViewH);
        [self.mainV addSubview:modView];
    }

    UIView *bottomBgView = [UIView ha_createView];
    bottomBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBgView];
    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(topBgV.mas_bottom).offset(10);
        make.height.mas_equalTo(180+60);
    }];
    
    UILabel *bottomTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:@"综合功能" alignment:0];
    [bottomBgView addSubview:bottomTitLabel];
    [bottomTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
        make.height.mas_offset(20);
    }];

    
    NSArray *titles = @[@"所有机具",@"机具下拨",@"下拨记录",@"机具回拨"];
    NSArray *subTitles = @[@"查询我及团队的机具",@"为团队下拨机具",@"下拨机具历史记录",@"撤回下级代理的机具"];
    NSArray *images = @[@"ha_25",@"ha_26",@"ha_27",@"ha_28"];
    
    for (int i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        NSString *subTitle = subTitles[i];
        NSString *img = images[i];

        UIView *itemView = [self setItmeView:title subTitle:subTitle img:img];
        itemView.tag = i;
        [bottomBgView addSubview:itemView];
        CGFloat itemW = kUIScreen_w*0.5;
        CGFloat itemH = 90;
        CGFloat itemX = 0;
        CGFloat itemY = 60;
        if (i == 0) {
            itemX = 0;
            itemY = 60;
        }else if (i == 1) {
            itemX = kUIScreen_w*0.5;
            itemY = 60;
        }else if (i == 2) {
            itemX = 0;
            itemY = 90+60;
        }else if (i == 3) {
            itemX = kUIScreen_w*0.5;
            itemY = 90+60;
        }
        itemView.frame = CGRectMake(itemX, itemY, itemW, itemH);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(itemViewClick:)];
        [itemView addGestureRecognizer:tap];
    }

}

-(UIView *)getModView:(NSInteger)type title:(NSString *)title bgColor:(UIColor *)bgColor {
    UIView *view = [UIView ha_createView];
    view.backgroundColor = bgColor;
    
    
    UILabel *numLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#ffffff" text:@"0" alignment:1];
    [view addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.height.mas_offset(14);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:title alignment:1];
    [view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(numLabel.mas_bottom).offset(10);
        make.height.mas_offset(14);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    [self.titLabelArray addObject:titLabel];
    [self.numLabelArray addObject:numLabel];
    return view;
}


-(UIView *)setItmeView:(NSString *)title subTitle:(NSString *)subTitle img:(NSString *)img {
    
    UIView *view = [UIView ha_createView];
    
    UIImageView *icon = [UIImageView ha_createImageView];
    icon.image = [UIImage imageNamed:img];
    [view addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
        make.width.mas_offset(icon.image.size.width);
        make.height.mas_offset(icon.image.size.height);
    }];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#111A34" text:title alignment:0];
    [view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(icon.mas_right).offset(10);
        make.centerY.mas_equalTo(-10);
        make.right.mas_equalTo(-10);
    }];

    UILabel *subTitLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#888888" text:subTitle alignment:0];
    [view addSubview:subTitLabel];
    [subTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(icon.mas_right).offset(10);
        make.centerY.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];

    return view;
}

- (UIView *)mainV
{
    if (!_mainV) {
        _mainV = [UIView ha_createView];
    }
    return _mainV;
}

- (NSMutableArray *)numLabelArray
{
    if (!_numLabelArray) {
        _numLabelArray = [NSMutableArray array];
    }
    return _numLabelArray;
}

- (NSMutableArray *)titLabelArray
{
    if (!_titLabelArray) {
        _titLabelArray = [NSMutableArray array];
    }
    return _titLabelArray;
}


@end
